package edu.gatech.app4args;

import edu.gatech.app4args.resources.Globals;
import edu.gatech.app4args.utils.SlideData;
import edu.gatech.app4args.utils.SlideDataSection;

import java.awt.Component;

import javax.swing.BoxLayout;
import javax.swing.JPanel;

/**
 * Controls the SlideInfoSections.
 *
 * @author andrew
 */
public class SlideInfoPanel extends JPanel{
	/** SlideInfoSection for the title attributes */
	private SlideInfoSection title;
	/** SlideInfoSection for the section1 attributes */
	private SlideInfoSection section1;
	/** SlideInfoSection for the section2 attributes */
	private SlideInfoSection section2;
	/** A reference to the Main class */
	private Main ref;
	/** The current slide number */
	private int slideNumber;

	/**
	 * Creates an instance of SlideInfoPanel, setting its layout and creating
	 * all info sections.
	 * 
	 * @param ref a reference to the Main class
	 */
	public SlideInfoPanel(Main ref){
		//setName("slideInfoPanel");
		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		title = new SlideInfoSection(this, Globals.section_title);
		section1 = new SlideInfoSection(this, Globals.section_content1);
		section2 = new SlideInfoSection(this, Globals.section_content2);

		this.ref = ref;
	}

	/**
	 * Sets up the panel with the appropriate sections for the given slide type.
	 *
	 * @param slidetype the slide type to setup sections for
	 */
	private void setup(int slidetype){
		this.removeAll();
		this.addSection(title, "TITLE");
		switch(slidetype){
			case Globals.sType_title:
				this.addSection(section1,"SUBTITLE");
				break;
			case Globals.sType_standard:
				this.addSection(section1,"TEXTAREA");
				break;
			case Globals.sType_two_content:
				this.addSection(section1,"TEXTAREA 1");
				this.addSection(section2,"TEXTAREA 2");
				break;
		}
		this.enableSlideInfo(true);
	}

	/**
	 * Loads the SlideInfoSections with the given SlideData objects attributes.
	 *
	 * @param slide the SlideData's attributes to load
	 * @param slideNumber the number of the slide being loaded
	 */
	public void load(SlideData slide, int slideNumber){
		this.slideNumber = slideNumber;
		this.setup(slide.getType());
		this.loadSection(title, slide.getTitle());
		this.loadSection(section1, slide.getContent1());
		if(slide.getType()==Globals.sType_two_content){
			this.loadSection(section2, slide.getContent2());
		}
		this.revalidate();
		this.repaint();
	}

	public int getSlideNumber() {
		return slideNumber;
	}
	/**
	 * Sets up and adds a SlideInfoSection using the given name.
	 *
	 * @param section
	 * @param name
	 */
	private void addSection(SlideInfoSection section, String name){
		section.setup(name);
		this.add(section);
	}

	/**
	 * Loads the SlideInfoSection with the liststyle and smartart saved in the
	 * SlideDataSection.
	 *
	 * @param siSection SideInfoSection to load
	 * @param sdSection SlideDatSection to load from
	 */
	private void loadSection(SlideInfoSection siSection, SlideDataSection sdSection){
		siSection.setListStyle(sdSection.getlStyle());
		siSection.setSmartArt(sdSection.getSmartArt());
	}

	/**
	 * Enables or disables all SlideInfoSections in the panel.
	 *
	 * @param enable true to enable, false to disable
	 */
	public void enableSlideInfo(boolean enable){
		for(Component c : this.getComponents()){
			((SlideInfoSection)c).enableSection(enable);
		}
	}

	/**
	 * Makes a call to Main to update the liststyle specifed by the
	 * SlideInfoSection.
	 *
	 * @param section the section to update
	 * @param liststyle the selected liststyle
	 */
	public void updateListStyle(int section, int liststyle){
		ref.updateListStyle(slideNumber, section, liststyle);
	}

	/**
	 * Makes a call to Main to update the smartart specified by the
	 * SlideInfoSection.
	 *
	 * @param section the section to update
	 * @param saStyle the selected smartart
	 */
	public void updateSmartArt(int section, int saStyle){
		ref.updateSmartArt(slideNumber, section, saStyle);
	}
}
